<template>
	<view>
		<view class="goods_list">
			<!-- 商品列表组件 -->
			<myitem v-for="(item,index) in goodsList" :key="index"
			 @gotodetail="godetail(item)" :goods="item"></myitem>
			<view class="endlist" v-if="total === goodsList.length">
				~~~~~人家也是有底线的~~~~~
			</view>
		</view>
	</view>
</template>

<script>
	import myitem from '../../components/my-goods/my-goods.vue'
	export default {
		data() {
			return {
				// 请求参数对象
				queryObj:{
					query:"",
					cid:"",
					pagenum:1,
					pagesize:10
				},
				goodsList:[],
				total:-1,
				isLoading:false//创建节流阀
			}
		},
		onLoad(e) {
			this.queryObj.query = e.query || ''
			this.queryObj.cid = e.cid || ''
			this.getGoodsList()
		},
		components:{
			myitem
		},
		//当滚动条滑到离底部150px触发加载数据函数
		onReachBottom() {
			if(this.queryObj.pagenum*this.queryObj.pagesize >= this.total) return uni.$showMsg("数据加载完毕")
			this.queryObj.pagenum +=1
			if(this.isLoading) return
			
			uni.showLoading({
				title:"数据加载中"
			})
			this.getGoodsList()
		},
		//下拉加载函数
		onPullDownRefresh() {
			this.queryObj.pagenum = 1
			this.total = -1
			this.isLoading = false
			this.goodsList = []
			this.getGoodsList(()=>uni.stopPullDownRefresh())//获取数据，结束后结束下拉刷新
		},
		methods: {
			//请求商品列表数据
			async getGoodsList(cb){
			this.isLoading = true
			cb && cb()
			 const {data : res}	= await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
			 if(res.meta.status!=200){
				 return uni.$showMsg()
			 }else{
				 uni.hideLoading()
				 this.goodsList = [...this.goodsList,...res.message.goods]
				 this.total = res.message.total
				 this.isLoading =false
			 }
			},
			//点击商品跳转到
			godetail(goods){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+goods.goods_id
				})
			}
		}
	}
</script>

<style>
.endlist{
	width: 100%;
	text-align: center;
	color: #bbb;
	font-size: 16px;
}
</style>
